<?php get_instance()->view('common/header');?>
<!--<script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=Tx7kgtgc31DjU25cP2FU0bFOr01MzI86"></script>-->
<script type="text/javascript" src="http://api.map.baidu.com/getscript?v=2.0&ak=DpM2LLeXhxIaS6jMmIGcCYo93rEvtRAV"></script>
<body class="bag3">
<div class="page">
    <div class="page__bd">
        <!--<a href="javascript:;" class="weui-btn weui-btn_primary">点击展现searchBar</a>-->
        <div class="weui-search-bar" id="searchBar">
            <form class="weui-search-bar__form">
                <div class="weui-search-bar__box">
                    <i class="weui-icon-search"></i>
                    <input type="search" class="weui-search-bar__input" id="searchInput" placeholder="搜索" required/>
                    <a href="javascript:" class="weui-icon-clear" id="searchClear"></a>
                </div>
                <label class="weui-search-bar__label" id="searchText">
                    <i class="weui-icon-search"></i>
                    <span>搜索</span>
                </label>
            </form>
            <a href="javascript:" class="weui-search-bar__cancel-btn" id="searchCancel">取消</a>
        </div>
    </div>
</div>
<div class="bag1">
    <p class="cor6 f08" style="padding:0.5em 3% 0;">当前地址</p>
    <ul class="p3">
        <li class="cor6 f08 p5_0 heng vCenter" >
            <span id="address_" class="f10" onclick="self.location=document.referrer;">
                <?php echo !empty($this->address) ? $this->address : '定位中...'?>
            </span>
            <span class="lanzi vCenter f10" onclick="reLocation();">
                <img class="heightimg mr5" src="<?php echo $this->staticUrl?>img/png/pic36.png" alt="">重新定位
            </span>
        </li>
    </ul>
</div>
<div class="mt5 bag1">
    <p class="cor6 f08 heng" style="padding:0.5em 3% 0;">收货地址<a href="<?php echo getUrl('user/address')?>" class="hongzi">地址管理</a></p>
    <?php if ($address):?>
    <ul class="p3">
        <?php foreach ($address as $v):?>
            <li class="cor6 f08 p5_0 bor_bb" onclick="chooseLocation(<?php echo $v['latitude'];?>, <?php echo $v['longitude'];?>);">
                <span class="f10"><?php echo $v['_address_str'];?></span>
                <p class="f09"><?php echo $v['_address'];?></p>
                <p class="f085 cor9 mt5">
                    <?php echo $v['consignee'];?>（<?php echo $v['gender_str'];?>） <?php echo $v['mobile'];?>
                </p>
            </li>
        <?php endforeach;?>

    </ul>
    <?php endif;?>
</div>
<div style="display: none">
    <p class="cor9 f09 bag3" style="padding:0.5em 3%;">附近地址</p>
    <ul class="p3" id="searchResult"></ul>
</div>
<div id="l-map" style="display: none"></div>
<div id='searchResultPanel' style="display: none"></div>
<?php get_instance()->view('common/wxJssdk');?>
</body>
<script src="<?php echo $this->staticUrl?>js/base.js"></script>
<script>
    function reLocation(){
        WeChat();
//        wx.getLocation({
//            type: 'wgs84',
//            success: function (res) {
//                // 地址解析:http://lbs.qq.com/javascript_v2/guide-service.html#link-four
//                let geocoder = new qq.maps.CityService({
//                    complete: function (result) {
//
//                        if(typeof setAddress == 'function'){
//
//                            setAddress(result.detail.latLng.lat, result.detail.latLng.lng);
//                        }
//                    }
//                });
//                var center = new qq.maps.LatLng(res.latitude, res.longitude);
//                geocoder.searchCityByLatLng(center)
//            }
//        })
    }

    function chooseLocation(lat, lng) {
        setAddress(lat, lng);
//        let geocoder = new qq.maps.CityService({
//            complete: function (result) {
//                if(typeof setAddress == 'function'){
//                    setAddress(lat, lng);
//                }
//            }
//        });
//        var center = new qq.maps.LatLng(lat, lng);
//        geocoder.searchCityByLatLng(center);

    }
    function setAddress(lat, lng){

        $.ajax('<?php echo getUrl('home/setLonLat');?>', {
            type:'post',
            data:{latitude:lat,longitude:lng,type:'wgs84'},
            dataType:'json',
            error:function(){
                blockUI.hide();
                show_message({status:0, message:'请求超时'});
            },
            timeout:60000,
            success:function(e,textStatus,jqXHR){
//                alert(123);
                $('#address_').text(e.data[2]);
                window.location.href = "<?php echo getUrl('home/index?reload=1');?>";

            }
        });
    }

    $(function(){
        var point = new BMap.Point(<?php echo $this->longitude ?>, <?php echo $this->latitude ?>);
        map = new BMap.Map("l-map");
        map.centerAndZoom(point, 16);
        map.enableScrollWheelZoom(true);
        var searchResult = $('#searchResult');
        var ac = new BMap.Autocomplete(    //建立一个自动完成的对象
            {"input" : "searchInput"
                ,"location" : map
            });

        function G(id) {
            return document.getElementById(id);
        }

        ac.addEventListener("onhighlight", function(e) {  //鼠标放在下拉列表上的事件
            var str = "";
            var _value = e.fromitem.value;
            var value = "";
            if (e.fromitem.index > -1) {
                value = _value.province +  _value.city +  _value.district +  _value.street +  _value.business;
            }
            str = "FromItem<br />index = " + e.fromitem.index + "<br />value = " + value;

            value = "";
            if (e.toitem.index > -1) {
                _value = e.toitem.value;
                value = _value.province +  _value.city +  _value.district +  _value.street +  _value.business;
            }
            str += "<br />ToItem<br />index = " + e.toitem.index + "<br />value = " + value;
            G("searchResultPanel").innerHTML = str;
//            $("#searchResultPanel").html(str)
        });

        var myValue;
        ac.addEventListener("onconfirm", function(e) {    //鼠标点击下拉列表后的事件
            var _value = e.item.value;
            myValue = _value.province +  _value.city +  _value.district +  _value.street +  _value.business;
            var del = "onconfirm<br />index = " + e.item.index + "<br />myValue = " + myValue;

            G("searchResultPanel").innerHTML = del;
//            $("#searchResultPanel").html(del)

            setPlace();
        });

        var init = true;
        map.addEventListener("tilesloaded",function(){

            if(init){
                var myGeo = new BMap.Geocoder();
                init = false;
            }
        });

        function setPlace(){
            map.clearOverlays();    //清除地图上所有覆盖物
            function myFun(){
                var pp = local.getResults().getPoi(0).point;    //获取第一个智能搜索的结果
                map.centerAndZoom(pp, 16);
                var lng = pp.lng;
                var lat = pp.lat;
                setLocation(lat, lng);
            }
            var local = new BMap.LocalSearch(map, { //智能搜索
                onSearchComplete: myFun
            });
            local.search(myValue);
        }

        function setLocation(lat, lng) {
            var address = $('#searchInput').val();

            $.ajax('<?php echo getUrl('home/setLonLat');?>', {
                type:'post',
                data:{address:address,latitude:lat,longitude:lng},
                dataType:'json',
                error:function(){
                    blockUI.hide();
                    show_message({status:0, message:'请求超时'});
                },
                timeout:60000,
                success:function(e,textStatus,jqXHR){
                    if (e.status == 1) {
                        window.location.href = e.url;
                    }
                    show_message(e);
                }
            });
        }

    });
</script>
</html>